package awk.script.javassist.method04.hotswap;

import javassist.CannotCompileException;
import javassist.NotFoundException;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import static net.bytebuddy.matcher.ElementMatchers.named;

/**
 * bytebuddy方式模拟双色球抽奖一刻改变号码（老百姓中不了一次，我也是瞎猜）
 * @author yujianlong
 * @create 2021-03-29 13:59
 **/
public class MockExampleBBTest {


	public static void main(String[] args) throws IOException, NotFoundException, CannotCompileException, InterruptedException, ClassNotFoundException {

		CompletableFuture.runAsync(
				new MyExample()
		);

		TimeUnit.SECONDS.sleep(1);

		ByteBuddyAgent.install();
		new ByteBuddy().redefine(MyExample.class)
				.method(named("myTest"))
				.intercept(  MethodDelegation.to(BBExampleProxy.class))
//				.intercept(FixedValue.value("Hello Foo Redefined"))
				.make()
				.load(Thread.currentThread().getContextClassLoader(), ClassReloadingStrategy.fromInstalledAgent());

		TimeUnit.SECONDS.sleep(30);
	}

}
